//
//  CreateAChatViewController.m
//  家校互通
//
//  Created by 李赛 on 15/7/13.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "CreateAChatViewController.h"
#import "ChartUsersTableViewCell.h"
#import "ChartUsersCollectionViewCell.h"
#import "ChartSwitchTableViewCell.h"
#import "quiteBtnTableViewCell.h"
#import "ClassMemberViewController.h"
@interface CreateAChatViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *studentList;
@end

@implementation CreateAChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _contentTableView.backgroundColor=[UIColor clearColor];
    _contentTableView.delegate=self;
    _contentTableView.dataSource=self;
    self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

    _studentList=[[NSMutableArray alloc]initWithCapacity:0];

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneForSelectStudent)];
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self connect];
}
- (void)connect{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [CoModel getChartUsers:^(BOOL result, NSMutableArray *roomList, NSError *error) {
        if (result) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            _studentList=[NSMutableArray arrayWithArray:roomList];
            [_contentTableView reloadData];
        }

    } withToken:[DSFUserInfo shareUserInstance].token andChartId:_chartId];
}


#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else if (section==1)
    {
        return 2;
    }else
    {
        return 1;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        ChartUsersTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"ChartUsersTableViewCell" forIndexPath:indexPath];
        vCell.contentCollectionView.delegate=self;
        vCell.contentCollectionView.dataSource=self;
        [vCell.contentCollectionView reloadData];
        return vCell;
    }
    else if(indexPath.section==1)
    {
        ChartSwitchTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"ChartSwitchTableViewCell" forIndexPath:indexPath];
        if (indexPath.row==0)
        {
            vCell.titleLB.text=@"消息免打扰";
            vCell.selectionStyle=UITableViewCellSelectionStyleNone;
            [vCell.switchBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventValueChanged];
        }
        else
        {
             vCell.selectionStyle=UITableViewCellSelectionStyleDefault;
            vCell.titleLB.text=@"清空聊天记录";
            vCell.switchBtn.hidden=YES;
        }
        return vCell;
    }else
    {
        quiteBtnTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"quiteBtnTableViewCell" forIndexPath:indexPath];
        
        [vCell.quiteBtn addTarget:self action:@selector(quiteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        return vCell;
    }

}
//消息免打扰
-(void)switchBtnAction:(UISwitch *)sender
{
    
}
//退出并删除
-(void)quiteBtnAction:(UIButton*)sender
{
    [TalkModel deleteClazzChart:^(BOOL result, NSError *error) {
        if (result) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    } withToken:[DSFUserInfo shareUserInstance].token andChartId:_chartId];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==0)
    {
        NSInteger itemCountsRow=DSF_SCREENWIDTH/60-1;/**<collectionView每行item个数*/
        NSInteger columnCount= (_studentList.count+1)/itemCountsRow+((_studentList.count+1)%itemCountsRow>0?1:0);/**<每行列数*/
        return  90*columnCount+10*(columnCount+1);
    }else if(indexPath.section==1)
    {
        return 44;
    }else
    {
        return 50;
    }

    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==1)
    {
        ChartSwitchTableViewCell *vCell=(ChartSwitchTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        if ([vCell.titleLB.text isEqualToString: @"清空聊天记录"]){
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"确定清空聊天记录么" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",@"取消", nil];
            [alertView show];
            
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }
        
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0) {
        NSLog(@"确定");
    }
}
#pragma mark---UICollectionViewDataSource--
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_studentList count]+1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChartUsersCollectionViewCell *vCell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ChartUsersCollectionViewCell" forIndexPath:indexPath];
    if (indexPath.row==_studentList.count)
    {
        vCell.userImage.image=[UIImage imageNamed:@"加号"];
        vCell.userNameLB.text=@"";
    }
    else
    {
      ChartUser *model = [_studentList objectAtIndex:indexPath.row];
      [vCell setModel:model];
    }
     return vCell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}
#pragma mark---UICollectionViewDelegate--
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==_studentList.count) {
        NSLog(@"跳转选择添加页面");
        ClassMemberViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"ClassMemberViewController"];
        vc.hasSelectedMember=_studentList;
        vc.chartId=_chartId;
        vc.title=self.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
